/*
 * Copyright (C) 2009 CodeWerkz
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.codewerkz.prompty;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.codewerkz.util.SettingsLoader;
import com.codewerkz.util.SettingsLoader.LoadSettings;
import com.codewerkz.util.TwoLineListAdapter;

/**
 * Settings class for Application.
 * TODO: XML STRINGS.
 * 
 * @author ChaZ
 */
public class Settings extends ListActivity  implements LoadSettings, OnClickListener {

	// Settings related variables.
	private static String[] sSettingsTitles =
						{
							"Font Settings",
							"Welcome Message",
							"Reset First Run"
						};
	private static String[] sSettingsDescriptions =
						{
							"Change the font size.",
							"Disable/Enable the welcome message.",
							"Enables any actions such as messages on first run"
						};
	
	/* MUST ACCOMODATE FOR ANY HEADERS OR FOOTERS (++ if added header) */
	private static final int SETTINGS_FONT = 1;
	private static final int SETTINGS_WELCOMEMESSAGE = 2;
	private static final int SETTINGS_FIRSTRUN = 3;
	
	private SettingsLoader mSettings;

	private Button mSaveButton;
	
	/**
	 * Runs when the Activity is first created.
	 * 
	 * @return void
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	// Load super class.
        super.onCreate(savedInstanceState);
        
        // Grab and set up the list.
        ListView list = getListView();
        list.setPadding(5, 5, 5, 5);
        list.setFooterDividersEnabled(false);
        list.setHeaderDividersEnabled(false);
        // Create settings header.
        TextView settingsHeader = new TextView(this);
        settingsHeader.setText("You may have to restart Prompty for the settings to take affect.");
        settingsHeader.setTextSize(TypedValue.COMPLEX_UNIT_PX, PresentationList.sFontSizePx);
        // Create a save button.
        mSaveButton = new Button(this);
        mSaveButton.setOnClickListener(this);
        mSaveButton.setText("Save");
        // Add them to the list.
        list.addHeaderView(settingsHeader, null, false);
        list.addFooterView(mSaveButton);
        
        // Load our settings.
        loadSettings();
        
        // Set the list adapter.
        setListAdapter(new TwoLineListAdapter(this, sSettingsTitles, sSettingsDescriptions));    
    }
    
    /**
     * Runs when paused and when paused.
     * 
     * @return void
     */
    @Override
    public void onPause() {
    	super.onPause();
    	mSettings.savePreferences();
    	finish();
    }
    
    /**
     * Runs when the activity is about to be destroyed and ready for Garbage Collection.
     * 
     * @return void
     */
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	mSettings = null;
    	mSaveButton = null;
    }
    
    /**
     * Interface function to loadSettings from Settings file.
     * 
     * @return void
     */
	@Override
	public void loadSettings() {
		// New SettingsLoader object.
		this.mSettings = new SettingsLoader(this);
	}
    
	/**
	 * OnClick method for save button.
	 * 
	 * @param v View ID of the view that has been pressed.
	 * @return void
	 */
	@Override
	public void onClick(View v) {
		if ( v == mSaveButton) {
			finish();
		}
		
	}

	/**
	 * Runs when the user clicks on an item in the list.
	 * 
	 * @param l ID of the list view.
	 * @param v View Id of the item.
	 * @param position Position of the item.
	 * @param id Id of the item.
	 */
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        
        // Switch the position
        switch(position) {
	        case SETTINGS_FONT:
	        	setFontSize();
	        	break;
	        case SETTINGS_WELCOMEMESSAGE:
	        	toggleWelcomeMessage();
	        	break;
	        case SETTINGS_FIRSTRUN:
	        	resetFirstRun();
	        	break;
        }
    }
    
    /**
     * Shows a dialog and lets the user select the font size.
     * 
     * @return void
     */
    private void setFontSize() {
    	// CharSequences to populate the dialog list with.
    	final CharSequence[] items = {"Large", "Default", "Small"};

    	// Create new AlertDialog with the builder.
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	// Set up the dialog (title, items, onclicklistener)
    	builder.setTitle("Font Size");
    	builder.setItems(items, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int item) {
    	    	try {
					mSettings.setFontSize(items[item].toString());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    	    }
    	});
    	// Create the alert.
    	AlertDialog alert = builder.create();
    	// Show it.
    	alert.show();
    	// Destroy it afterwards.
    	builder = null;
    }
    
    /**
     * Shows a dialog and lets the user select the
     * 
     * @return void
     */
    private void toggleWelcomeMessage() {
    	final CharSequence[] items = {"Enabled", "Disabled"};

    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Welcome Message");
    	builder.setItems(items, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int item) {
    	    	if(items[item].toString() == "Enabled") {
    	    		mSettings.setWelcomeMessageEnabled(true);
    	    	} else {
    	    		mSettings.setWelcomeMessageEnabled(false);
    	    	}
    	    }
    	});
    	AlertDialog alert = builder.create();
    	alert.show();
    }
    
    /**
     * Resets the application to as if it is first run to show any first run
     * messages etc.
     */
    private void resetFirstRun()
    {
    	mSettings.setFirstRun(true);
    }
}